package mongoDB;

import java.util.Iterator;

import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.util.JSON;

public class MyMongoTest {
	public static void main(String[] args) throws Exception {
		/*
		 * MongoClientOptions mcs =
		 * MongoClientOptions.builder().connectionsPerHost(6).connectTimeout(
		 * 1000).build(); MongoClient mongo = new
		 * MongoClient(" 192.168.6.134:27017",mcs);
		 * System.out.println("ConnectionsPerHost=="+mongo.getMongoClientOptions
		 * ().getConnectionsPerHost());
		 */

		MongoClient mongo = new MongoClient(" 10.1.10.77", 10002);

		DB db = mongo.getDB("mydb");

		DBCollection users = db.getCollection("tuser");
		
		// db.appUser.aggregate([{"$group":{_id:"$userid",count:{$sum:1}}},{$sort:{count:-1}}])
		BasicDBObject d1 = new BasicDBObject();

		d1.put("uname", "myU12");
		d1.put("userId2", 0);
		d1.append("userId", "myU2").append("name", "updatename222");
		users.insert(d1);

		// BasicDBObjectBuilder builder =
		// BasicDBObjectBuilder.start().add("userId", "u3").add("age", 123);

		// String json = "{'userId':'t234','age':234}";
		// DBObject d1 = (DBObject)JSON.parse(json);

		// users.insert(d1);
		// users.insert(builder.get());

		// users.remove(new BasicDBObject());

		// users.update(new BasicDBObject("userId", "myU2"),d1);
		// users.update(new BasicDBObject("userId", "myU2"),
		// new BasicDBObject("$set",
		// new BasicDBObject("name","newName")
		// )
		// );

		// DBObject d = users.findOne();
		// System.out.println("d=="+d);

		// {userId:"ttuu" , age:{$gte:100,$lt:200} }

		/*
		 * DBObject con = new BasicDBObject( "userId","ttuu").append( "age", new
		 * BasicDBObject("$gt",120).append("$lt", 210) );
		 * 
		 * DBObject show = new
		 * BasicDBObject("userId",1).append("age",1).append("_id", 0);
		 * 
		 * DBCursor c = users.find(con,show).limit(2); while(c.hasNext()){
		 * DBObject d = c.next(); System.out.println("d==="+d); }
		 */

		mongo.close();

	}
}
